/*
* @(#) Recursion.java 2018年02月06日
*
* Copyright 2010 NetEase.com, Inc. All rights reserved.
*/
package com.leo.m1802.d06;

import java.util.Arrays;

/**
 *
 * @author xuexiaolei
 * @version 2018年02月06日
 */
public class Recursion {
    /**
     * 斐波那契数列
     * @param n
     * @return
     */
    public int f(int n){
        if (n<=1) {return 1;}
        return f(n-1)+f(n-2);
    }

    /**
     * 汉诺塔
     * @param n
     * @param from
     * @param to
     * @param tmp
     */
    public void hanio(int n, char from, char to, char tmp){
        if (n>0){
            hanio(n-1, from, tmp, to);
            System.out.println("take "+ n +" from " + from +" to " + to);
            hanio(n-1, tmp, to, from);
        }
    }

    public static void p(int x){
        if (x > 0){
            System.out.print(x%10);
            p(x/10);
        }else {
            System.out.println();
        }
    }

    public static void pp(int x){
        System.out.print(x%10);
        if (x >= 10){
            pp(x/10);
        }else {
            System.out.println();
        }
    }

    private void pai(int[] s, int start, int end){
        if (start==end){
            System.out.println(Arrays.toString(s));
        }else {
            for (int i = start; i <= end; i++) {
                swap(s, start, i);
                pai(s, start+1, end);
                swap(s, start, i);
            }
        }
    }
    public void pai(int[] s){
        pai(s, 0, s.length-1);
    }
    private void swap(int[] a, int i, int j){
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

    private void zu(int[] s, boolean[] sb, int start, int end){
        if (start == end){
            for (int i = 0; i < sb.length; i++) {
                if (sb[i]) {System.out.print(s[i]+" ");}
            }
            System.out.println();
        }else {
            sb[start] = true;
            zu(s, sb, start+1, end);
            sb[start] = false;
            zu(s, sb, start+1, end);
        }
    }
    public void  zu(int[] s){
        zu(s, new boolean[s.length], 0, s.length);
    }

    public static void main(String[] args) {
        p(10);
        p(12312);
        p(0);
        pp(10);
        pp(12312);
        pp(0);
        Recursion r = new Recursion();
        r.pai(new int[]{1,2,3});
        r.zu(new int[]{1,2,3});
    }
}
